package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.ModulesBO;
import com.junzhiit.nengji.cms.service.ModulesService;
import com.junzhiit.nengji.cms.web.exception.ModulesNotFoundException;
import com.junzhiit.nengji.cms.web.util.OrderNumberUtils;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ModulesAddVO;
import com.junzhiit.nengji.cms.web.vo.ModulesEditVO;
import com.junzhiit.nengji.cms.web.vo.ModulesListVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.ModulesDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Modules;
import com.junzhiit.nengji.model.Shop;

@Service
public class ModulesServiceImpl extends BaseServiceImpl implements ModulesService {

	@Autowired
	private ModulesDAO modulesDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, ModulesListVO modulesListVO,String sessionId) {
		Modules modules = new Modules();
		BeanUtils.copyProperties(modulesListVO, modules);
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = null;
		if(StringUtils.isNotBlank(systemUserSessionVO.getSystemId())){
			shop = shopDAO.query(systemUserSessionVO.getSystemId());
		}
		if (shop != null) {
			modules.setShopId(shop.getId());
		}

		List<ModulesBO> list = modulesDAO.findAll(modules, dtp.getStart(), dtp.getLength());
		int totalCount = modulesDAO.getTotalCount(modules);

		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO add(ModulesAddVO modulesAddVO,String sessionId) {
		ResultVO result = new ResultVO();
		Modules modules = new Modules();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = null;
		if(StringUtils.isNotBlank(systemUserSessionVO.getSystemId())){
			shop = shopDAO.query(systemUserSessionVO.getSystemId());
		}
		String maxCode = "";
		if (shop != null) {
			modules.setShopId(shop.getId());
			maxCode = modulesDAO.getMaxCode(shop.getId());
		}else{
			maxCode = modulesDAO.getMaxCode(null);
		}
		BeanUtils.copyProperties(modulesAddVO, modules);
		String code = getModulesCode(maxCode);
		modules.setCode(code);
		
		Modules data = modulesDAO.insert(modules);
		result.setData(data);

		return result;
	}

	@Override
	public ResultVO edit(ModulesEditVO modulesEditVO) {
		ResultVO result = new ResultVO();
		Modules modules = new Modules();
		BeanUtils.copyProperties(modulesEditVO, modules);
		boolean data = modulesDAO.update(modules);
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO findById(String id) {
		ResultVO result = new ResultVO();
		Modules modules = modulesDAO.query(id);
		if (modules == null) {
			throw new ModulesNotFoundException("模块不存在");
		}
		result.setData(modules);
		return result;
	}

	@Override
	public ResultVO del(String id) {
		ResultVO result = new ResultVO();
		Modules modules = modulesDAO.query(id);
		if (modules == null) {
			throw new ModulesNotFoundException("模块不存在");
		}
		boolean data = modulesDAO.updateToDelete(id);
		result.setData(data);
		return result;
	}

	private String getModulesCode(String maxCode) {
		int codeValue = 1;
		if (StringUtils.isNotBlank(maxCode)) {
			int currentMaxCode = Integer.valueOf(maxCode.substring(maxCode.length() - 3, maxCode.length()));
			codeValue += currentMaxCode;
		}
		String code = OrderNumberUtils.getSystemCode(OrderNumberUtils.MODULES_PREFIX, codeValue);
		return code;
	}

	@Override
	public ResultVO getAll(){
		ResultVO result=new ResultVO();
		List<Modules> data=modulesDAO.getAll();
		result.setData(data);
		return result;
	}
}
